package day4.HomeWork;

public class Test5 {
    /*
    定义方法getNum(String type, int... numbers)，根据type参数对可变参数numbers执行指定计算：
    支持操作类型："sum"（求和）、"avg"（算术平均）、"max"（最大值）、"min"（最小值）等
    处理空参数集时返回 0
    输出格式示例：getNum("avg", 2,4,6) → 输出 平均值是：4.0
    */
    public  static double getNum(String type, int... numbers){
        // 处理空参数集
        if (numbers == null || numbers.length == 0) {
            return 0;
        }
        switch (type){
            case "sum":
                double sum = 0;
                for (int i = 0; i <numbers.length ; i++) {
                    sum+=numbers[i];
                }
                return sum;
            case "avg":
                double sum2 = 0;
                for (int i = 0; i <numbers.length ; i++) {
                    sum2+=numbers[i];
                }
                return sum2/numbers.length;
            case "max":
                int max = numbers[0];
                for (int i = 0; i < numbers.length; i++) {
                    if(numbers[i]>max){
                        max = numbers[i];
                    }
                }
                return max;
            case "min":
                int min = numbers[0];
                for (int i = 0; i < numbers.length; i++) {
                    if(numbers[i]<min){
                        min = numbers[i];
                    }
                }
                return min;
            default:
                return 0;
        }
    }
    public static void main(String[] args) {
        double result = getNum("avg", 2,4,6);
        System.out.println("平均值为："+result);
        double result2 = getNum("avg");
        System.out.println("空参数时："+result2);
    }

}
